define(['knockout', 'util', 'mArchive'], function (ko, Util, MArchive) {
    function ViewModel(params) {
        Util.extend(this, params);

        var scope = this;
        var baseHash = '#/ArchiveDepartment&' + JSON.js2Uri(params);
        scope.leftCard = {
            title: '分类',
            css:{'xgx-col1-left-card':true},
            titleButtons: [],
            onOperation: function () {
                var modelDefault = {departmentName: ko.root.user.trueName};

                var params2 = Util.getCmpFromHash(2).params;

                if (params2) {
                    modelDefault.departmentName =params2.departmentName;
                    modelDefault.classifyCode = params2.classifyCode;
                    modelDefault.year = params2.year;
                }

                var archive = new MArchive({isKo: true, default: modelDefault});

                archive.setProxy({url:archive.getProxy().url.formatTpl(params.code)});
                archive.setStoreId('archiveStore');

                ko.root.openModal({size:'lg-1100',
                    operation: 'addArchive', record: archive, title: '添加档案',
                    commit: function (btn, record, callback) {
                        record.save(function(data){
                            if(data.success) ko.storeCache['archiveTree'].load();
                            callback(data);
                        });
                    }
                });
            },
            cardBody: {
                name: 'cmp:Tree',
                params: {
                    store:{url: '/dhxy/rest/basearchives/' + params.code + '/tree',id:'archiveTree'},
                    dataFormat: function (data) {
                        if (data.classifyCode) {
                            //data.href = baseHash+'/ArchiveList&'+Util.JS2URI($.extend({code:params.code},data));
                        }
                        else {
                            //data.href = baseHash+'/ArchiveStatistic';
                            data.selectable = false;
                        }
                    },
                    onNodeSelected: function (data) {
                        location.hash = baseHash + '/ArchiveList&' + JSON.js2Uri($.extend({code: params.code}, data));
                    },
                    onNodeUnselected: function (data) {
                        location.hash = baseHash + '/ArchiveStatistic';
                    }
                }
            }
        };

        var addArchive = ko.root.menus.findItemByField({value:'addArchive',name:'name'});

        scope.leftCard.titleButtons.pushItem(addArchive);
    }

    return ViewModel;
});
